<?php 
/** 
 * 翻转单词顺序列
 * 解答：1.先将整个句子翻转
 * 		 2.再以空格隔开为每个小单词，再对每个小单词翻转 		 		
 */

function ReverseSentence($str)
{
	if (!$str) {
		return '';
	}
	//先将整个句子翻转
	$str = reserve($str);
    $str_arr = explode(' ', $str);
    $ret_str = '';
    foreach ($str_arr  as $key => $value) {
    	$ret_str .= reserve($value).' ';
    }
    var_dump($ret_str);
}


function reserve($str)
{
	$left = 0;
	$right = strlen($str) - 1;
	while($left < $right) {
		$temp = $str[$left];
		$str[$left] = $str[$right];
		$str[$right] = $temp;
		$left++;
		$right--;
	}
	return $str;
}


$str = 'I am a student';

ReverseSentence($str);